<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃      
 * Date: 2016-06-21
 */

namespace Seller\Controller;

class ReportController extends BaseController{
	public $begin;
	public $end;
	public $store_id;
	public function _initialize(){
        parent::_initialize();
		$timegap = I('timegap');
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin = $gap[0];
			$end = $gap[1];
		}else{
			$lastweek = date('Y-m-d');//30天前
			$begin = I('begin',$lastweek);
			$end =  I('end',date('Y-m-d'));
		}
		$this->begin = strtotime($begin);
		$this->end = strtotime($end)+86399;
		$this->store_id = STORE_ID;
		$this->assign('timegap',date('Y-m-d',$this->begin).' - '.date('Y-m-d',$this->end));
	}
	
	public function index(){
		$now = strtotime(date('Y-m-d'));
		$today['today_amount'] = M('order')->where("add_time>=$now AND (pay_status=1 or pay_code='cod') and store_id=$this->store_id and order_status in(1,2,4)")->sum('order_amount');//今日销售总额
		$today['today_order'] = M('order')->where("add_time>=$now and store_id=$this->store_id and (pay_status=1 or pay_code='cod')")->count();//今日订单数
		$today['cancel_order'] = M('order')->where("add_time>=$now AND order_status=3 and store_id=$this->store_id")->count();//今日取消订单
		$today['sign'] = round($today['today_amount']/$today['today_order'],2);
		$this->assign('today',$today);
		$sql = "SELECT COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap from  __PREFIX__order ";
		$sql .= " where add_time>$this->begin and add_time<$this->end and store_id=$this->store_id AND (pay_status=1 or pay_code='cod') and order_status in(1,2,4) group by gap";
		$res = M()->query($sql);//订单数,交易额
		
		foreach ($res as $val){
			$arr[$val['gap']] = $val['tnum'];
			$brr[$val['gap']] = $val['amount'];
			$tnum += $val['tnum'];
			$tamount += $val['amount'];
		}

		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
			$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
			$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);						
			$order_arr[] = $tmp_num;
			$amount_arr[] = $tmp_amount;			
			$sign_arr[] = $tmp_sign;
			$date = date('Y-m-d',$i);
			$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
			$day[] = $date;
		}
		
		$this->assign('list',$list);
		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
		$this->assign('result',json_encode($result));
		$this->display();
	}

	// public function index(){
	// 	$now = strtotime(date('Y-m-d'));
	// 	$today['today_amount'] = M('account_log')->where("change_time>$now and storeid=$this->store_id")->sum('user_money');//今日销售总额
	// 	$today['today_order'] = M('account_log')->where("change_time>$now and storeid=$this->store_id")->count();//今日订单数
	// 	$today['cancel_order'] = 0;//M('account_log')->where("change_time>$now AND order_status=3 and store_id=$this->store_id")->count();//今日取消订单
	// 	$today['sign'] = round($today['today_amount']/$today['today_order'],2);
	// 	$this->assign('today',$today);
	// 	$sql = "SELECT COUNT(*) as tnum,sum(user_money) as amount, FROM_UNIXTIME(change_time,'%Y-%m-%d') as gap from  __PREFIX__account_log ";
	// 	$sql .= " where change_time>$this->begin and change_time<$this->end and storeid=$this->store_id  group by gap";
	// 	$res = M()->query($sql);//订单数,交易额
		
	// 	foreach ($res as $val){
	// 		$arr[$val['gap']] = $val['tnum'];
	// 		$brr[$val['gap']] = $val['amount'];
	// 		$tnum += $val['tnum'];
	// 		$tamount += $val['amount'];
	// 	}

	// 	for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
	// 		$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
	// 		$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
	// 		$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);						
	// 		$order_arr[] = $tmp_num;
	// 		$amount_arr[] = $tmp_amount;			
	// 		$sign_arr[] = $tmp_sign;
	// 		$date = date('Y-m-d',$i);
	// 		$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
	// 		$day[] = $date;
	// 	}
		
	// 	$this->assign('list',$list);
	// 	$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
	// 	$this->assign('result',json_encode($result));
	// 	$this->display();
	// }
	public function saleTop(){
		$sql = "select goods_name,goods_sn,sum(goods_num) as sale_num,sum(goods_num*goods_price) as sale_amount from __PREFIX__order_goods ";
		$sql .=" where is_send = 1 and store_id=$this->store_id group by goods_id order by sale_amount DESC limit 100";
		$res = M()->cache(true,3600)->query($sql);
		$this->assign('list',$res);
		$this->display();
	}
	

	public function saleList(){
		$p = I('p',0);
		$start = $p*20;
		$sql = "select a.*,b.order_sn,b.shipping_name,b.pay_name,b.add_time from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql .= " where b.add_time>$this->begin and b.add_time<$this->end and a.store_id=$this->store_id order by add_time limit $start,20";
		$res = M()->cache(true)->query($sql);
		$this->assign('list',$res);
		
		$sql2 = "select count(*) as tnum from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql2 .= " where b.add_time>$this->begin and b.add_time<$this->end and a.store_id=$this->store_id";
		$total = M()->query($sql2);
		$count =  $total[0]['tnum'];
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$this->assign('page',$show);
		$this->display();
	}
	
	//财务统计
	public function finance(){
		$sql = "SELECT sum(b.goods_num*b.member_goods_price) as goods_amount,sum(a.shipping_price) as shipping_amount,sum(b.goods_num*b.cost_price) as cost_price,";
		$sql .= "sum(a.coupon_price) as coupon_amount,FROM_UNIXTIME(a.add_time,'%Y-%m-%d') as gap from  __PREFIX__order a left join __PREFIX__order_goods b on a.order_id=b.order_id ";
		$sql .= " where a.add_time>$this->begin and a.add_time<$this->end AND a.store_id=$this->store_id and a.pay_status=1 and a.shipping_status=1 and b.is_send=1 group by gap order by a.add_time";
		$res = M()->cache(true)->query($sql);//物流费,交易额,成本价
		
		foreach ($res as $val){
			$arr[$val['gap']] = $val['goods_amount'];
			$brr[$val['gap']] = $val['cost_price'];
			$crr[$val['gap']] = $val['shipping_amount'];
			$drr[$val['gap']] = $val['coupon_amount'];
		}
			
		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$date = $day[] = date('Y-m-d',$i);
			$tmp_goods_amount = empty($arr[$date]) ? 0 : $arr[$date];
			$tmp_cost_amount = empty($brr[$date]) ? 0 : $brr[$date];
			$tmp_shipping_amount = empty($crr[$date]) ? 0 : $crr[$date];
			$tmp_coupon_amount = empty($drr[$date]) ? 0 : $drr[$date];
			
			$goods_arr[] = $tmp_goods_amount;
			$cost_arr[] = $tmp_cost_amount;
			$shipping_arr[] = $tmp_shipping_amount;
			$coupon_arr[] = $tmp_coupon_amount;
			$list[] = array('day'=>$date,'goods_amount'=>$tmp_goods_amount,'cost_amount'=>$tmp_cost_amount,
					'shipping_amount'=>$tmp_shipping_amount,'coupon_amount'=>$tmp_coupon_amount,'end'=>date('Y-m-d',$i+24*60*60));
		}
		$this->assign('list',$list);
		$result = array('goods_arr'=>$goods_arr,'cost_arr'=>$cost_arr,'shipping_arr'=>$shipping_arr,'coupon_arr'=>$coupon_arr,'time'=>$day);
		$this->assign('result',json_encode($result));
		$this->display();
	}
	
	public function visit(){
		$this->display();
	}
	/*public function jixiao(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d',strtotime("-1 month"));//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = strtotime( I('end',date('Y-m-d')));
			
		}
		$seller =session('seller');
		$store_id=$seller['store_id'];
	
		$list=M('seller')->field('ty_seller.seller_id,ty_users.nickname,ty_seller_group.group_name')->join('ty_users on ty_seller.user_id=ty_users.user_id')->join('ty_seller_group on ty_seller.group_id =ty_seller_group.group_id')->where(array('ty_seller.store_id'=>$store_id,'ty_seller.is_admin'=>0))->order('ty_seller.group_id DESC')->select();
		
		for($i=0;$i<count($list);$i++){
			$czy_id=$list[$i]['seller_id'];
			//var_dump($czy_id);
			$sql="select count(id) as cishu  from ty_liliao where czy_id=$czy_id and  shijian >=$begin and shijian <=$end";
			$arr=M()->query($sql);
			$sql1="select count(user_id) as renshu  from ty_users where tuixiao_id=$czy_id and  reg_time >=$begin and reg_time <=$end";
			$arr1=M()->query($sql1);
			//var_dump($arr);
			$list[$i]['cishu']=$arr['0']['cishu'];
			$list[$i]['renshu']=$arr1['0']['renshu'];
		}

		$this->assign('list',$list);
		$this->display();
	}*/


	public function doneday(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('seller');
		$store_id=$seller['store_id'];
		$storeinfo = M('store')->field('store_name')->where("store_id=$store_id")->find();

		$list=M('seller')->field('ty_seller.seller_id,ty_users.user_id,ty_users.nickname,ty_seller_group.group_name')->join('ty_users on ty_seller.user_id=ty_users.user_id')->join('ty_seller_group on ty_seller.group_id =ty_seller_group.group_id')->where(array('ty_seller.store_id'=>$store_id,'ty_seller.is_admin'=>0))->order('ty_seller.group_id DESC')->select();
		$total_list = array();
		for($i=0;$i<count($list);$i++){
			$czy_id=$list[$i]['user_id'];

			//var_dump($czy_id);
			/*每个人时间段内服务次数*/
			$sql="select count(id) as cishu  from ty_liliao where czy_id=$czy_id and  shijian >=$begin and shijian <=$end";
			$arr=M()->query($sql);
			/*每个人时间段内进馆人数*/
			$sql1="select count(user_id) as renshu from ty_users where tuixiao_id=$czy_id and  reg_time >=$begin and reg_time <=$end";
			$arr1=M()->query($sql1);
			/*每个人时间段内回款数*/
			$sql2="select sum(total_amount) as total_amount from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status in(1,2,4)";
			$arr2=M()->query($sql2);
			$sql3="select sum(total_amount) as total_amount,sum(yajin) as yajin from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status=6";
			$arr3=M()->query($sql3);

			/*每个人时间段内办理疗卡张数*/
			$goods_info="select order_id from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status in(1,2,4,6)";
			$arr4=M()->query($goods_info);
			$card_num = 0;
			foreach ($arr4 as $key => $v) {
				$card_num_n = M('order_goods')->field('goods_num')->where("order_id = ".$v['order_id'].' and (goods_name like "%理疗%" or goods_name like "%泥灸%")')->find();
				$card_num += $card_num_n['goods_num'];
			}
			
			$list[$i]['card_num']=$card_num;
			$list[$i]['cishu']=$arr['0']['cishu'];
			$list[$i]['renshu']=$arr1['0']['renshu'];
			$total_n = $arr2['0']['total_amount'];
		 	$total_b_in = $arr3['0']['total_amount'];
		 	$total_b_out = $arr3['0']['total_amount']-$arr3['0']['yajin'];
			$list[$i]['total_amount']=$total_n+$total_b_in-$total_b_out;
			$list[$i]['total_out']=$total_b_out;

			/*总计*/
			$t_card_num += $card_num;
			$t_cishu += $arr['0']['cishu'];
			$t_renshu += $list[$i]['renshu'];
			$t_b_in += $list[$i]['total_amount'];
			$t_b_out += $list[$i]['total_out'];
		}
		$total_list = array(
			't_card_num'=>$t_card_num,
			't_cishu'=>$t_cishu,
			't_renshu'=>$t_renshu,
			't_b_in'=>$t_b_in,
			't_b_out'=>$t_b_out,
		);
		$this->assign('total_list',$total_list);
		$this->assign('storeinfo',$storeinfo);
		$this->assign('list',$list);
		$this->display();
	}


	public function countday(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('seller');
		$store_id=$seller['store_id'];
		$storeinfo = M('store')->field('store_name')->where("store_id=$store_id")->find();
		
		$class = M('goods_category')->field('id,name')->where("level=3")->select();
		$class_id = array();
		$class_name = array();
		foreach ($class as $key => $v) {
			$class_id[] = $v['id'];
			$class_name[$v['id']] = $v['name'];
		}
		$class_id = implode(',', $class_id);
		
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 in ($class_id)")->select();
	
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[$v['cat_id3']][] = $v['goods_id'];
		}
		//根据分类的商品id查询总金额欠款等
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = $store_id and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

		foreach ($class_goods_f as $key => $v) {
			$vn = implode(',', $v);

			$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
			foreach ($order_detail as $ke => $va) {
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($vn)")->find();
				$classname = $class_name[$key];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
				$class_total_hk .= "[".$va['pay_code']."]";

			}
			
			$order_count_detail[$key] = array(
				'class_id' => $key,
				'classname' => $classname,
				'classnum' => $classnum,
				'class_total_fee' => $class_total_fee,
				'class_total_yajin' => $class_total_yajin,
				'class_total_qk' => $class_total_qk,
				'class_total_hk' => $class_total_hk,
			);
			$total_fee += $class_total_fee;
			$total_num += $classnum;
			$total_yajin += $class_total_yajin;
			$total_qk += $class_total_qk;
		}

		$total_list = array(
				'total_num' => $total_num,
				'total_fee' => $total_fee,
				'total_yajin' => $total_yajin,
				'total_qk' => $total_qk,
			);
		$this->assign('total_list',$total_list);
		$this->assign('order_count_detail',$order_count_detail);
		$this->assign('storeinfo',$storeinfo);

		$this->display();
	}	
	

	public function countday_detail(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;

		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}

		$class_id = I('class_id');
		$seller =session('seller');
		$store_id=$seller['store_id'];
		$storeinfo = M('store')->field('store_name')->where("store_id=$store_id")->find();
		
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 =$class_id")->select();
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[] = $v['goods_id'];
		}
		$goods_id_t = implode(',', $class_goods_f);
		//根据分类的商品id查询总金额欠款等
		
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = $store_id and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

			

			foreach ($order_detail as $ke => $va) {
				$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($goods_id_t)")->find();
				
				$classname = $order_goods_detail['goods_name'];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
				$class_total_hk .= "[".$va['pay_code']."]";
				$order_count_detail[] = array(
					'class_id' => $class_id,
					'classname' => $classname,
					'classnum' => $classnum,
					'class_total_fee' => $class_total_fee,
					'class_total_yajin' => $class_total_yajin,
					'class_total_qk' => $class_total_qk,
					'class_total_hk' => $class_total_hk,
				);
				$total_fee += $class_total_fee;
				$total_num += $classnum;
				$total_yajin += $class_total_yajin;
				$total_qk += $class_total_qk;
			}
			
			
			
	
		$total_list = array(
				'total_num' => $total_num,
				'total_fee' => $total_fee,
				'total_yajin' => $total_yajin,
				'total_qk' => $total_qk,
			);
		$this->assign('total_list',$total_list);
		$this->assign('order_count_detail',$order_count_detail);
		$this->assign('storeinfo',$storeinfo);
		$this->assign('class_id',$class_id);

		$this->display();
	}	
}